$(function() {
	var StudentGrid = $("#StudentGrid");
	var zhuanBanGrid=$("#zhuanBanGrid");
	var studentGridAction=$("#studentGridAction");
	StudentGrid.datagrid({
		url : 'student/list',
		fit : true,
		border : false,
		rownumbers : true,
		pagination : true, // 是否显示分页栏
		fitColumns : false,
		columns : [ [
				{
					field : 'user_name',
					title : '姓名',
					align : 'center',
					width : 80
				},
				{
					field : 'gender',
					title : '性别',
					align : 'center',
					width : 50,
					formatter : function(value, row, index) {
						if (value == 1) {
							value = '<img style="width:35px; height:35px" src="public/images/girl.png">';
							return value;
						} else if (value == 2) {
							value = '<img style="width:40px; height:40px" src="public/images/boy.png">';
							return value;
						}
					}
				},
				{
					field : 'class_name',
					title : '所属班级',
					align : 'center',
					width : 100

				},
				{
					field : 'contact_num',
					title : '手机号',
					align : 'center',
					width : 100
				},
				{
					field : 'address',
					title : '学生地址',
					align : 'center',
					width : 150,
					formatter : function(value) {
						if (value==null||value=='') {
							return "<span  style='color:red;'  title='暂无'>暂无</span>";
						}else{
							return "<span title='" + value + "'>" + value
							+ "</span>";
						}
					}
				},
				{
					field : 'parent_name',
					title : '父/母亲姓名',
					align : 'center',
					width : 100,
					formatter : function(value) {
						if (value==null||value=='') {
							return "<span  style='color:red;'  title='暂无'>暂无</span>";
						}else{
							return "<span title='" + value + "'>" + value
							+ "</span>";
						}
					}
				},
				{
					field : 'parent_phone',
					title : '父/母亲电话',
					align : 'center',
					width : 100,
					formatter : function(value) {
						if (value==null||value=='') {
							return "<span  style='color:red;'  title='暂无'>暂无</span>";
						}else{
							return "<span title='" + value + "'>" + value
							+ "</span>";
						}
					}
				},
				{
					field : 'entrance_time',
					title : '入学时间',
					sortable : true,
					align : 'center',
					width : 120,
					formatter : function(value, row, index) {
						var c = new Date(value);// 传入一个时间戳
						c.toLocaleString();// 返回本地时间
						var d = new Date(c);
						var formatIt = d.getFullYear() + '-'
								+ (d.getMonth() + 1) + '-' + d.getDate() + ' '
								+ d.getHours() + ':' + d.getMinutes() + ':'
								+ d.getSeconds();
						return formatIt;
					}
				},
				{
					field : 'total_credits',
					title : '总学分',
					align : 'center',
					width : 70
				},
				{
					field : 'state',
					title : '状态',
					align : 'center',
					width : 60,
					formatter : function(value, row, index) {
						if (value == 1) {
							return "在读中";
						} else if (value == 2) {
							return "已退学";
						} else if (value == 3) {
							return "就业中";
						} else if (value == 4) {
							return "已就业";
						}
						return value;
					}
				},
				{
					field : 'ac',
					title : '操作',
					align : 'center',
					width : 350,
					formatter : function(value, row, index) {
					/*	var bts = [];
						bts.push('<a data-index="' + index
								+ '" class="ctr edit">修改基本信息</a>');
						bts.push('<a data-index="' + index
								+ '" class="ctr zhuanbuzhang">转为部长</a>');
						bts.push('<a data-index="' + index
								+ '" class="ctr biye">毕业</a>');
						bts.push('<a data-index="' + index
								+ '" class="ctr zhuanban">转班</a>');
						bts.push('<a data-index="' + index
								+ '" class="ctr fangtan">访谈</a>');
						bts.push('<a data-index="' + index
								+ '" class="ctr studentInfo">个人详情</a>');*/
						return studentGridAction.children("a.ctr").attr('data-index', index)
								.end().html();;
					}
				} ] ],
		toolbar :"#studentGridToolbar"
	});
	// 模糊查询
	// 检索按钮
	var studentName = $("#name");// 获取姓名
	var className = $("#class");// 获取班级
	var gender = $("#sex");// 获取姓别
	var state = $("#state");// 获取状态

	$("#studentSearchForm").on('click', function() {
		StudentGrid.datagrid("load", {
			classId : className.val() == '请选择' ? '' : className.val(),
			address : "%" + studentName.val() + "%",
			gender : gender.val() == '请选择' ? '' : gender.val(),
			state : state.val() == '请选择' ? '' : state.val()
		});
	});
	// 给重置按钮添加事件
	var studentForm = $("#studentForm");
	$("#studentReset").on('click', function() {
		studentForm.form('clear');
		StudentGrid.datagrid("load", {});
	});
	var gridPanel = StudentGrid.datagrid("getPanel");
	gridPanel.on("click", "a.edit", function() {
		var index = this.dataset.index;
		var rows = StudentGrid.datagrid("getRows");
		var id = rows[index].studentinfo_id;
		Studentform(id);
	}).on("click", "a.zhuanbuzhang", function() {
		var index = this.dataset.index;
		var rows = StudentGrid.datagrid("getRows");
		var id = rows[index].studentinfo_id;
		var x=confirm("确定要转成部长？");
		if (x) {
			BuZhang(id);
		}
	}).on("click", "a.biye", function() {
		var index = this.dataset.index;
		var rows = StudentGrid.datagrid("getRows");
		var id = rows[index].studentinfo_id;
		biye(id);
	}).on("click", "a.zhuanban", function() {
		var index = this.dataset.index;
		var rows = StudentGrid.datagrid("getRows");
		var id = rows[index].studentinfo_id;
		ZhuanBan(id);
	}).on("click", "a.fangtan", function() {
		var index = this.dataset.index;
		var rows = StudentGrid.datagrid("getRows");
		var id = rows[index].studentinfo_id;
		fangtan(id);
	}).on("click", "a.studentInfo", function() {
		var index = this.dataset.index;
		var rows = StudentGrid.datagrid("getRows");
		var id = rows[index].studentinfo_id;
		//alert("学生编号是："+id);
		ChaXunPersonInfo(id);
	}).on("click", "a.create", function() {
		Studentform();
	})
	function ChaXunPersonInfo(id) {
		var bol = $('#tabs').tabs('exists', '学生个人信息');
		if (bol) {
			$('#tabs').tabs('select', '学生个人信息')
		} else {
			$('#tabs').tabs('add', {
				title : '学生个人信息',
				closable : true,
				href : 'jiaoxue/jx-studentinfo/'+id
			});
		}
	}

	function BuZhang(id){
		alert("确定将该学生转为部长？");
			$.get("student/buzhang/"+id,function(rs){
				if (rs.success) {
					$.messager.alert("提示", "操作成功");
					// 刷新数据表格
					StudentGrid.datagrid("reload");
					
				}else{
					$.messager.alert("提示", "操作失败！");
				}
			});
	}
	function biye(id){
		alert("确定执行这一操作？");
		$.get("student/biye/"+id,function(rs){
			if (rs.success) {
				// 刷新数据表格
				StudentGrid.datagrid("reload");
			}else{
				$.messager.alert("提示", "操作失败！");
			}
		});
}
	
	function Studentform(id) {
		var dialog = $("<div/>").dialog(
				{
					href : 'jiaoxue/jx-add-student' + (id ? ("/" + id) : ''),
					title : '添加学生',
					height : '567',
					width : '580',
					modal : true,
					onClose : function() {
						// 窗口关闭的同时销毁此窗口
						$(this).dialog("destroy");
					},

					buttons : [ {
						iconCls : 'icon-save',
						text : '添加',
						handler : function() {
							// 获取到表单对象
							var addStudentForm = $("#addStudentForm");
							// 校验表单
							if (addStudentForm.form('validate')) {
								// serialize方法可以获取到表单所有数据
								$.post("student/save", addStudentForm
										.serialize(), function(rs) {
									if (rs.success) {
										// 刷新数据表格
										StudentGrid.datagrid("reload");
										// 关闭窗口
										dialog.dialog("close");
									} else {
										$.messager.alert("提示", rs.message);
									}
								});
							}
						}
					} ]
				});
	}

	function ZhuanBan(id) {
		var dialog = $("<div/>")
				.dialog(
						{
							href : 'jiaoxue/zhuanban'
									+ (id ? ("/" + id) : ""),
							title : '添加转班记录',
							height : '300',
							width : '500',
							modal : true,
							onClose : function() {
								$(this).dialog("destroy");
							},
							buttons : [
									{
										iconCls : 'icon-save',
										text : '保存',
										handler : function() {
											// 获取表单中的内容
											var addZhuanBanForm = $("#addZhuanBanForm");
											if (addZhuanBanForm
													.form('validate')) {
												$
														.post(
																"zhuanban/save",
																addZhuanBanForm
																		.serialize(),
																function(rs) {
																	if (rs.success) {
																		/*zhuanBanGrid.datagrid("reload");
																		// 刷新数据表格
																		StudentGrid .datagrid("reload");
																		// 关闭窗口
																		dialog.dialog("close");*/
																		StudentGrid.datagrid("reload");
																		dialog.dialog("close");
																	} else {
																		$.messager
																				.alert(
																						"提示",
																						rs.message);
																	}
																});
											}
										}
									}, {
										iconCls : 'icon-clear',
										text : '取消',
										handler : function() {
											dialog.dialog("close");
										}
									} ]
						});
	}

	function fangtan(id) {
		var dialog = $("<div/>").dialog(
				{
					href : 'jiaoxue/jx-add-fangtan' + (id ? ("/" + id) : ""),
					title : '访谈',
					height : '450',
					width : '507',
					modal : true,
					onClose : function() {
						$(this).dialog("destroy");
					},
					buttons : [{
								iconCls : 'icon-save',
								text : '添加',
								handler : function() {
									var addFangtanForm = $("#addFangtanForm");
									if (addFangtanForm.form('validate')) {
										$.post("fangtan/save", addFangtanForm
												.serialize(), function(rs) {
											if (rs.success) {
												StudentGrid.datagrid("reload");
												dialog.dialog("close");
											} else {
												$.messager.alert("提示",
														rs.message);
											}
										});
									}
								}
							}, {
								iconCls : 'icon-clear',
								text : '取消',
								handler : function() {
									dialog.dialog("close");
								}
							} ]
				});
	}
});